package com.baiyang.server.control.system;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.baiyang.server.model.transaction.Logistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import com.baiyang.server.annotation.Jurisdiction;
import com.baiyang.server.model.PageEntity;
import com.baiyang.server.model.system.Menu;
import com.baiyang.server.model.system.Route;
import com.baiyang.server.service.system.RouteService;
import com.baiyang.server.service.system.UserRoleService;
import com.baiyang.server.tools.Letter;
import com.baiyang.server.tools.DataTool;
import com.baiyang.server.tools.SubjectNotNull;

@RestController
@RequestMapping(value = "/routeOperation")
@Jurisdiction(value = "routeOperation")
public class RouteCon {
	@Autowired
	private RouteService routeService = null;
	@Autowired
	private UserRoleService userRoleService = null;
	/**
	 * 根绝条件分页获取数据
	 * @return
	 */
	@Jurisdiction(value = "getPage")
	@RequestMapping(value = "/getPage",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Route> getPage(@RequestBody JSONObject object){
		Letter<Route> letter = new Letter<Route>();
		try {
			PageEntity<Route> result = routeService.getPage(object);
			letter.setList(result.getDatas());
			letter.setTotal(result.getCount());
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}
	
	/**
	 *  获取用户路由信息
	 * @param request
	 * @return
	 */
	@Jurisdiction(value = " getRoutes")
	@RequestMapping(value = "/getRoutes",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Route> getRoutes(HttpServletRequest request){
		Letter<Route> letter = new Letter<Route>();
		try {
			String userId  = DataTool.getUserIdFromSession(request);
			if(DataTool.isBlank(userId)){
				letter.setResult("ERROR");
				letter.setDetail("数据获取失败！");
				return letter;
			}
			List<String> roleIds = userRoleService.selectIdsByUserId(userId);
			List<Route> routes = routeService.getRoutes(roleIds);
			Route root = new Route();
			root = (Route) DataTool.getTree(routes, root);
			letter.setData(root);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}
	
	/**
	 * 根绝条件获取数据
	 * @return
	 */
	@Jurisdiction(value = "selectList")
	@RequestMapping(value = "/selectList",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Route> selectList(@RequestBody Route route){
		Letter<Route> letter = new Letter<Route>();
		try {
			List<Route> result = routeService.selectList(route);
			letter.setList(result);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}
	
	/**
	 * 根据条件获取一个
	 * @param route
	 * @return
	 */
	@Jurisdiction(value = "selectOne")
	@RequestMapping(value = "/selectOne",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Route> selectOne(@RequestBody Route route){
		Letter<Route> letter = new Letter<Route>();
		try {
			Route obj = routeService.selectOne(route);
			letter.setData(obj);
			letter.setDetail("数据获取成功！");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}
	
	/**
	 * 插入数据
	 * @param route
	 * @return
	 */
	@Jurisdiction(value = "insert")
	@RequestMapping(value = "/insert",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insert(@RequestBody Route route){
		Letter<String> letter = new Letter<String>();
		route.setId(DataTool.createUUID());
		Map<String, String> sub = SubjectNotNull.subject(route);
		if(sub.size() != 0) {
			letter.setResult("FAIL");
			letter.setDetail("必填信息不能为空！" + sub.toString());
			return letter;
		}
		try {
			routeService.insert(route);
			letter.setDetail("数据插入成功！");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}
	
	/**
	 * 批量插入数据
	 * @param routes
	 * @return
	 */
	@Jurisdiction(value = "insertBatch")
	@RequestMapping(value = "/insertBatch",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insertBatch(@RequestBody List<Route> routes){
		Letter<String> letter = new Letter<String>();
		try {
			routeService.insertBatch(routes,letter);
			letter.setResult("SUCCESS");
			letter.setDetail("数据插入成功！");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}
	
	/**
	 * 根绝条件进行删除
	 * @param route
	 * @return
	 */
	@Jurisdiction(value = "delete")
	@RequestMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> delete(@RequestBody Route route){
		Letter<String> letter = new Letter<String>();
		if(SubjectNotNull.subjectIsNull(route)) {
			letter.setResult("FAIL");
			letter.setDetail("信息不能为空！");
			return letter;
		}
		try {
			routeService.delete(route);
			letter.setDetail("数据删除成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据删除失败！");
		}
		return letter;
	}

	/**
	 * 根据id批量删除
	 */
	@Jurisdiction(value = "deletes")
	@RequestMapping(value = "/deletes",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> deletes(@RequestBody List<Route> routes){
		Letter<String> letter = new Letter<String>();
		if(DataTool.isBlank(routes)) {
			letter.setResult("FAIL");
			letter.setDetail("信息不能为空！");
			return letter;
		}
		try {
			routeService.deletes(routes);
			letter.setDetail("数据删除成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据删除失败！");
		}
		return letter;
	}

	/**
	 * 根绝id更新
	 * @param route
	 * @return
	 */
	@Jurisdiction(value = "updateById")
	@RequestMapping(value = "/updateById",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> updateById(@RequestBody Route route){
		Letter<String> letter = new Letter<String>();
		if(route.getId().equals("")) {
			letter.setResult("FAIL");
			letter.setDetail("id不能为空！");
			return letter;
		}
		try {
			routeService.updateById(route);
			letter.setDetail("数据修改成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据修改失败！");
		}
		return letter;	
	}
}
